package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountLogMapper;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Slf4j
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;
    @Autowired
    private AccountLogMapper accountLogMapper;

    @Override
    @Transactional
    public void decrIntergral(OperateIntergralVo operateIntergralVo) {
        //往t_account_log表插入数据
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setGmtTime(new Date());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setType(AccountLog.TYPE_DECR);
        accountLogMapper.insert(accountLog);
        //减少积分
        int count = usableIntegralMapper.decrIntergral(operateIntergralVo.getValue(),operateIntergralVo.getUserId());
        if (count == 0){
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    @Transactional
    public void incrIntergral(OperateIntergralVo operateIntergralVo) {
        //往t_account_log表插入数据
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setGmtTime(new Date());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setType(AccountLog.TYPE_INCR);
        accountLogMapper.insert(accountLog);
        //增加积分
        usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(),operateIntergralVo.getValue());
    }

    @Override
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        log.info("进入到try");
        //往account_log表中插入数据，要让cancel知道有没有执行过try
        common(operateIntergralVo,context,AccountTransaction.STATE_TRY,AccountTransaction.INCR_TYPE);
    }

    @Override
    public void incrIntergralCommit(BusinessActionContext context) {
        log.info("进入到confirm");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null){
            log.info("异常情况");
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())){
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            //执行confirm业务逻辑
            usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(),operateIntergralVo.getValue());
            //修改状态为已提交
            accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId()+"",AccountTransaction.STATE_COMMIT,AccountTransaction.STATE_TRY);
        }else if (AccountTransaction.STATE_COMMIT.equals(accountTransaction.getState())){
            //说明之前已经是执行过confirm，不做任何事情处理幂等性的情况
        }else {
            log.info("异常情况");
        }
    }

    @Override
    public void incrIntergralRollback(BusinessActionContext context) {
        log.info("进入到cancel");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(),context.getBranchId()+"");
        if (accountTransaction == null){
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            //插入一条记录，状态为已回滚
            common(operateIntergralVo,context,AccountTransaction.STATE_CANCEL,AccountTransaction.INCR_TYPE);
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())){
            //执行cancel逻辑
            //把状态修改为已提交
            accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId()+"",AccountTransaction.STATE_CANCEL,AccountTransaction.STATE_TRY);
        }else if (AccountTransaction.STATE_CANCEL.equals(accountTransaction.getState())){
            //处理幂等性不做任何事情
        }else {
            log.info("异常情况");
        }
    }

    public void common(OperateIntergralVo operateIntergralVo, BusinessActionContext context,Integer state,int type){
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setActionId(context.getBranchId()+"");
        accountTransaction.setAmount(operateIntergralVo.getValue());
        accountTransaction.setGmtCreated(new Date());
        accountTransaction.setGmtModified(new Date());
        accountTransaction.setState(state);
        accountTransaction.setTxId(context.getXid());
        accountTransaction.setUserId(operateIntergralVo.getUserId());
        accountTransaction.setType(type+"");
        accountTransactionMapper.insert(accountTransaction);
    }

}
